﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace SGCG.Model
{
    public class Triangle : CompositeBorderedGraphicObject
    {
        private Line _AB, _AC, _BC;

        public Triangle(GraphicObject parent) : base(parent) { }

        protected override System.Collections.Generic.IEnumerable<GraphicObject> BuildObject()
        {
            _AB = new Line(this);
            _AC = new Line(this);
            _BC = new Line(this);

            yield return _AB;
            yield return _AC;
            yield return _BC;
        }

        public int X1
        {
            get { return this._AB.X1; }

            set
            {
                this._AB.X1 = value;
                this._AC.X1 = value;
            }
        }

        public int X2
        {
            get { return this._AB.X2; }

            set
            {
                this._AB.X2 = value;
                this._BC.X1 = value;
            }
        }

        public int X3
        {
            get { return this._AC.X2; }

            set
            {
                this._AC.X2 = value;
                this._BC.X2 = value;
            }
        }

        public int Y1
        {
            get { return this._AB.Y1; }

            set
            {
                this._AB.Y1 = value;
                this._AC.Y1 = value;
            }
        }

        public int Y2
        {
            get { return this._AB.Y2; }

            set
            {
                this._AB.Y2 = value;
                this._BC.Y1 = value;
            }
        }

        public int Y3
        {
            get { return this._AC.Y2; }

            set
            {
                this._AC.Y2 = value;
                this._BC.Y2 = value;
            }
        }

        protected void CopyProperties(Triangle other)
        {
            base.CopyProperties(other);
            other.X1 = X1;
            other.Y1 = Y1;
            other.X2 = X2;
            other.Y2 = Y2;
            other.X3 = X3;
            other.Y3 = Y3;
        }

        public override GraphicObject CreateCopy()
        {
            Triangle triangle = new Triangle(null);
            CopyProperties(triangle);
            return triangle;
        }

        public int ComputedX1 { get; private set; }
        public int ComputedY1 { get; private set; }
        public int ComputedX2 { get; private set; }
        public int ComputedY2 { get; private set; }
        public int ComputedX3 { get; private set; }
        public int ComputedY3 { get; private set; }

        private void Recompute()
        {
            Point p1 = TransformPoint(new Point(X1, Y1));
            ComputedX1 = (int)Math.Round(p1.X);
            ComputedY1 = (int)Math.Round(p1.Y);
            Point p2 = TransformPoint(new Point(X2, Y2));
            ComputedX2 = (int)Math.Round(p2.X);
            ComputedY2 = (int)Math.Round(p2.Y);
            Point p3 = TransformPoint(new Point(X3, Y3));
            ComputedX3 = (int)Math.Round(p3.X);
            ComputedY3 = (int)Math.Round(p3.Y);
        }

        protected override Point GetBarycenter()
        {
            Recompute();
            return new Point(
                (double)(ComputedX1 + ComputedX2 + ComputedX3) / 3,
                (double)(ComputedY1 + ComputedY2 + ComputedY3) / 3);
        }
    }
}
